package com.kakarote.oa.controller;


import com.alibaba.fastjson.JSONObject;
import com.kakarote.core.common.ApiExplain;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.feign.admin.entity.SimpleUser;
import com.kakarote.oa.entity.BO.LogBO;
import com.kakarote.oa.entity.BO.LogSaveBO;
import com.kakarote.oa.entity.BO.OaLogExportBO;
import com.kakarote.oa.entity.PO.OaLogRule;
import com.kakarote.oa.entity.VO.OaBusinessNumVO;
import com.kakarote.oa.entity.VO.OaLogTemplateFieldVO;
import com.kakarote.oa.entity.VO.OaLogVO;
import com.kakarote.oa.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 工作日志表 前端控制器
 * </p>
 *
 * @author wyq
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/oaLog")
@Api(tags = "日志模块")
public class OaLogController {

    @Autowired
    private IOaLogService oaLogService;

    @Autowired
    private IOaLogRuleService oaLogRuleService;

    @Autowired
    private IOaCommonService oaCommonService;

    @Autowired
    private IOaLogUserFavourService oaLogUserFavourService;

    @PostMapping("/initOaData")
    @ApiExplain("初始化日志数据")
    public Result<Boolean> initOaData() {
        return Result.ok(oaCommonService.initOaData());
    }

    @PostMapping("/initCalendarData")
    @ApiExplain("初始化日历数据")
    public Result<Boolean> initCalendarData() {
        return Result.ok(oaCommonService.initCalendarData());
    }

    @PostMapping("/initOaExamineData")
    @ApiExplain("初始化OA审批数据")
    public Result<Boolean> initOaExamineData() {
        return Result.ok(oaCommonService.initOaExamineData());
    }

    @PostMapping("/queryList")
    @ApiOperation("日志列表")
    public Result<BasePage<OaLogVO>> queryList(@RequestBody LogBO bo) {
        return Result.ok(oaLogService.queryLogList(bo));
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("修改或新增")
    public Result addOrUpdate(@RequestBody LogSaveBO jsonObject) {
        oaLogService.saveAndUpdate(jsonObject);
        return Result.ok();
    }

    @PostMapping("/information/{id}")
    @ApiOperation("查询日志详情信息")
    public Result<List<OaLogTemplateFieldVO>> information(@PathVariable("id") @ApiParam(name = "id", value = "id") Long id) {
        List<OaLogTemplateFieldVO> information = oaLogService.information(id);
        return Result.ok(information);
    }

    @PostMapping("/v2/queryList")
    @ApiOperation("日志列表v2")
    public Result<BasePage<OaLogVO>> queryLogList(@RequestBody LogBO bo) {
        return Result.ok(oaLogService.queryLogList(bo));
    }

    @PostMapping("/favourOrCancel")
    @ApiOperation("用户点赞或取消")
    public Result<JSONObject> favourOrCancel(@RequestParam("isFavour") boolean isFavour, @RequestParam("logId") Long logId) {
        return Result.ok(oaLogUserFavourService.userFavourOrCancel(isFavour, logId));
    }

    /**
     * 获取日志欢迎语
     */
    @PostMapping("/getLogWelcomeSpeech")
    @ApiOperation("获取日志欢迎语")
    public Result<String> getLogWelcomeSpeech() {
        String value = oaLogService.getLogWelcomeSpeech();
        return Result.ok(value);
    }

    @PostMapping("/queryLogBulletin")
    @ApiOperation("查询日志统计信息")
    public Result<JSONObject> queryLogBulletin() {
        JSONObject object = oaLogService.queryLogBulletin();
        return Result.ok(object);
    }

    @PostMapping("/queryCompleteStats")
    @ApiOperation("查询日志完成情况统计")
    public Result<JSONObject> queryCompleteStats(@RequestParam("configId") Long configId) {
        JSONObject object = oaLogService.queryCompleteStats(configId);
        return Result.ok(object);
    }

    @PostMapping("/queryCompleteOaLogList")
    @ApiOperation("查询日志完成情况统计列表")
    public Result<BasePage<JSONObject>> queryCompleteOaLogList(@RequestBody LogBO bo) {
        BasePage<JSONObject> basePage = oaLogService.queryCompleteOaLogList(bo);
        return Result.ok(basePage);
    }

    @PostMapping("/queryIncompleteOaLogList")
    @ApiOperation("查询日志未完成情况统计列表")
    public Result<BasePage<SimpleUser>> queryIncompleteOaLogList(@RequestBody LogBO bo) {
        BasePage<SimpleUser> basePage = oaLogService.queryIncompleteOaLogList(bo);
        return Result.ok(basePage);
    }

    @PostMapping("/deleteById")
    @ApiOperation("删除")
    public Result deleteById(@RequestParam("logId") Long logId) {
        oaLogService.deleteById(logId);
        return Result.ok();
    }

    @PostMapping("/queryLogBulletinByType")
    @ApiOperation("查询日志完成情况统计列表2")
    public Result<BasePage<JSONObject>> queryLogBulletinByType(@RequestBody LogBO bo) {
        BasePage<JSONObject> basePage = oaLogService.queryLogBulletinByType(bo);
        return Result.ok(basePage);
    }

    @PostMapping("/queryLogRecordCount")
    @ApiOperation("查询日志记录统计")
    public Result<List<JSONObject>> queryLogRecordCount(String logId, Integer today, @RequestParam(value = "sortField", required = false) String sortField, @RequestParam(value = "order", required = false) Integer order) {
        List<JSONObject> objectList = oaLogService.queryLogRecordCount(logId, today, sortField, order);
        return Result.ok(objectList);
    }

    @PostMapping("/queryById")
    @ApiOperation("根据日志id获取日志")
    public Result<JSONObject> queryById(@RequestParam("logId") Long logId) {
        JSONObject oaLog = oaLogService.queryById(logId);
        return Result.ok(oaLog);
    }

    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(@RequestBody OaLogExportBO logBO, HttpServletResponse response) {
        oaLogService.exportLog(logBO, response);
    }

    @PostMapping("/queryOaLogRuleList")
    @ApiOperation("查询日志规则")
    public Result<List<OaLogRule>> queryOaLogRuleList() {
        List<OaLogRule> logRules = oaLogRuleService.queryOaLogRuleList();
        return Result.ok(logRules);
    }

    /**
     * 设置日志提交设置
     */
    @PostMapping("/setOaLogRule")
    @ApiOperation("设置日志提交设置")
    public Result setOaLogRule(@RequestBody List<OaLogRule> ruleList) {
        oaLogRuleService.setOaLogRule(ruleList);
        return Result.ok();
    }

    /**
     * app端
     *
     * @return
     */
    @PostMapping("/queryOaBusinessNum")
    @ApiOperation("app端某个查询数量功能")
    public Result<OaBusinessNumVO> queryOaBusinessNum() {
        OaBusinessNumVO businessNumVO = oaLogService.queryOaBusinessNum();
        return Result.ok(businessNumVO);
    }

    @PostMapping("/updateLogRelId")
    @ApiOperation("更换日志关联信息ID")
    public Result updateLogRelId(Integer type, Long oldId, Long newId) {
        oaLogService.updateLogRelId(type, oldId, newId);
        return Result.ok();
    }
}

